package com.yc.springmvc;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

public class MyWebAppInit implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext cxt = new AnnotationConfigWebApplicationContext();
        cxt.register(MVCConfig.class);

        DispatcherServlet dispatcherServlet = new DispatcherServlet((WebApplicationContext) cxt);
       // MyDispatchServlet dispatcherServlet = new MyDispatchServlet();

        ServletRegistration.Dynamic registration = servletContext.addServlet("app", dispatcherServlet);
        registration.setLoadOnStartup(1);
        registration.addMapping("/");  // 映射所有地址

        // 添加文件上传配置
        MultipartConfigElement mc = new MultipartConfigElement("/");
        mc.getLocation();     // 临时文件存放目录
        mc.getMaxFileSize();  // 最大文件大小
        mc.getMaxRequestSize();  // 最大請求大小(总体大小)
        mc.getFileSizeThreshold();  // 保存临时文件的文件大小边界值
        registration.setMultipartConfig(mc);
    }
}
